import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/getx/all_controller_binding.dart';
import 'package:flutter_app/getx/controller.dart';
import 'package:flutter_app/getx/lazy_controller.dart';
import 'package:flutter_app/openeye/widget/app_bar.dart';
import 'package:flutter_app/stateLearn/click_txt.dart';
import 'package:get/get.dart';

import 'other.dart';

class GetXTest extends StatelessWidget {
  const GetXTest({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
      var find = Get.find<Controller>();
    return materialPage(
        appBar: appBar("getX测试页面", actions: createNextWidget()),
        page: createHomePage(find));
  }

  createLazyControllerPage() {
    var one = Get.find<LazyController>(tag: "one");
    var two = Get.find<LazyController>(tag: "two");
    var three = Get.find<LazyController>(tag: "three");
    var allControllerBinding = AllControllerBinding();
    allControllerBinding.dependencies();
    return Column(
      children: [
      Row(
        children: [
          Flexible(child: TextField(
            onChanged: (value){
              one.name.value = value;
            },
          )),
          Flexible(child: Obx(() =>Text(one.name.value))),
        ],
      ),
        Row(
          children: [
            Flexible(child: TextField(
              onChanged: (value){
                two.name.value = value;
              },
            )),
            Flexible(child: Obx(() =>Text(two.name.value))),
          ],
        ),
        Row(
          children: [
            Flexible(child: TextField(
              onChanged: (value){
                three.name.value = value;
              },
            )),
            Flexible(child: Obx(() =>Text(three.name.value))),
          ],
        ),
        Obx(() => Text("存入的值one:${one.name.value},two:${two.name.value},three:${three.name.value}"))
      ],
    );
  }
  createHomePage(Controller controller) {
    return Container(
      alignment: Alignment.center,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ClickTxt(
            info: "增加字面量",
            onPressed: () => controller.increment(),
          ),
          ClickTxt(
            info: "改变按钮显示",
            onPressed: () => controller.changeButtonStatus(),
          ),
          Obx(() => Text("${controller.count}")),
          Obx(() => controller.isShowButton.value
              ? const Text("按钮显示")
              : const Text("")),
          Obx(() => Text(controller.userName.value)),
          TextField(
            decoration: const InputDecoration(
              helperText: "请输入用户名",
              labelText: "姓名:",
              labelStyle: TextStyle(color: Colors.red),
            ),
            onChanged: (value) {
              print(value);
              controller.user.update((val) {
                val!.name = value;
              });
              controller.userName.value = value;
            },
          ),
          beanTest(),
        ],
      ),
    );
  }

  createNextWidget() {
    return [
      InkWell(
        child: const Icon(Icons.navigate_next, color: Colors.black),
        onTap: () {
          Get.to(const Other());
        },
      )
    ];
  }

  beanTest() {
    final controller = Get.find<Controller>();
    return Column(children: [
      Obx(() => Text("姓名:${controller.user.value.name}年龄:${controller.user.value.age}")),
      TextField(
        decoration: const InputDecoration(
            labelText: "年龄:",
            helperText: "请输入年龄",
            labelStyle: TextStyle(color: Colors.red)),
        inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[0-9]"))],
        keyboardType: TextInputType.number,
        onChanged: (value) {
          print(value);
          controller.user.update((val) {
            val!.age = int.parse((value.isEmpty)?"0":value);
          });
        },
      ),
    ]);
  }

}
